package org.lanyu.springainovel.common.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lanyu.springainovel.common.entity.TaskNotification;
import org.lanyu.springainovel.common.service.TaskNotificationService;
import org.lanyu.springainovel.common.util.UserAuthUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务通知控制器 - 合并了异步任务和通知功能
 */
@Slf4j
@RestController
@RequestMapping("/task-notification")
@RequiredArgsConstructor
@Tag(name = "任务通知管理", description = "任务通知管理接口，用于管理系统中的任务通知")
public class TaskNotificationController {

    private final TaskNotificationService taskNotificationService;

    private final UserAuthUtil userAuthUtil;

    @Operation(summary = "获取未读通知数量", description = "获取当前用户的未读通知数量")
    @GetMapping("/unread-count")
    public ResponseEntity<Map<String, Object>> getUnreadCount() {
        // 这里应该从SecurityContext获取当前用户ID，暂时使用固定值
        Long userId = userAuthUtil.getCurrentUserId();
        
        int unreadCount = taskNotificationService.getUnreadCount(userId);
        int unreadTaskCount = taskNotificationService.getUnreadTaskCount(userId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("unreadCount", unreadCount);
        result.put("unreadTaskCount", unreadTaskCount);
        
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "获取所有通知", description = "获取当前用户的所有通知")
    @GetMapping("/all")
    public ResponseEntity<List<TaskNotification>> getAllNotifications() {
        Long userId = userAuthUtil.getCurrentUserId();
        List<TaskNotification> notifications = taskNotificationService.getAllByUserId(userId);
        return ResponseEntity.ok(notifications);
    }

    @Operation(summary = "获取未读通知", description = "获取当前用户的未读通知")
    @GetMapping("/unread")
    public ResponseEntity<List<TaskNotification>> getUnreadNotifications() {
        Long userId = userAuthUtil.getCurrentUserId();
        List<TaskNotification> notifications = taskNotificationService.getUnreadByUserId(userId);
        return ResponseEntity.ok(notifications);
    }

    @Operation(summary = "获取活动任务", description = "获取当前用户的活动任务")
    @GetMapping("/active-tasks")
    @Deprecated
    public ResponseEntity<List<TaskNotification>> getActiveTasks() {
        Long userId = userAuthUtil.getCurrentUserId();
        List<TaskNotification> tasks = taskNotificationService.getActiveTasksByUserId(userId);
        return ResponseEntity.ok(tasks);
    }

    @Operation(summary = "获取已完成任务", description = "获取当前用户的已完成任务")
    @GetMapping("/completed-tasks")
    @Deprecated
    public ResponseEntity<List<TaskNotification>> getCompletedTasks() {
        Long userId = userAuthUtil.getCurrentUserId();
        List<TaskNotification> tasks = taskNotificationService.getCompletedTasksByUserId(userId);
        return ResponseEntity.ok(tasks);
    }

    @Operation(summary = "根据任务类型获取活动任务", description = "根据任务类型获取当前用户的活动任务")
    @GetMapping("/active-tasks/{taskType}")
    @Deprecated
    public ResponseEntity<List<TaskNotification>> getActiveTasksByType(
            @Parameter(description = "任务类型") @PathVariable String taskType) {
        Long userId = userAuthUtil.getCurrentUserId();
        List<TaskNotification> tasks = taskNotificationService.getActiveTasksByUserIdAndType(userId, taskType);
        return ResponseEntity.ok(tasks);
    }

    @Operation(summary = "标记通知为已读", description = "将指定通知标记为已读")
    @PutMapping("/read/{id}")
    public ResponseEntity<Map<String, Object>> markAsRead(
            @Parameter(description = "通知ID") @PathVariable Long id) {
        boolean success = taskNotificationService.markAsRead(id);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        
        if (success) {
            result.put("message", "标记成功");
        } else {
            result.put("message", "标记失败");
        }
        
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "标记所有通知为已读", description = "将当前用户的所有通知标记为已读")
    @PutMapping("/read-all")
    public ResponseEntity<Map<String, Object>> markAllAsRead() {
        Long userId = userAuthUtil.getCurrentUserId();
        boolean success = taskNotificationService.markAllAsRead(userId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        
        if (success) {
            result.put("message", "标记成功");
        } else {
            result.put("message", "标记失败");
        }
        
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "删除已读通知和任务", description = "删除当前用户的所有已读通知和任务")
    @DeleteMapping("/read-tasks")
    public ResponseEntity<Map<String, Object>> deleteReadTasks() {
        Long userId = userAuthUtil.getCurrentUserId();
        boolean success = taskNotificationService.deleteReadTasks(userId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        
        if (success) {
            result.put("message", "删除成功");
        } else {
            result.put("message", "删除失败");
        }
        
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "删除所有已读通知", description = "删除当前用户的所有已读通知")
    @DeleteMapping("/read-notifications")
    public ResponseEntity<Map<String, Object>> deleteReadNotifications() {
        Long userId = userAuthUtil.getCurrentUserId();
        boolean success = taskNotificationService.deleteReadNotifications(userId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        
        if (success) {
            result.put("message", "删除成功");
        } else {
            result.put("message", "删除失败");
        }
        
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "删除旧已完成任务和通知", description = "删除指定天数前的已完成任务和通知")
    @DeleteMapping("/old-completed-tasks/{days}")
    public ResponseEntity<Map<String, Object>> deleteOldCompletedTasks(
            @Parameter(description = "天数") @PathVariable int days) {
        Long userId = userAuthUtil.getCurrentUserId();
        boolean success = taskNotificationService.deleteOldCompletedTasks(userId, days);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        
        if (success) {
            result.put("message", "删除成功");
        } else {
            result.put("message", "删除失败");
        }
        
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "删除通知", description = "删除指定通知")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteNotification(
            @Parameter(description = "通知ID") @PathVariable Long id) {
        boolean success = taskNotificationService.deleteById(id);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        
        if (success) {
            result.put("message", "删除成功");
        } else {
            result.put("message", "删除失败");
        }
        
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "获取通知详情", description = "获取指定通知的详细信息")
    @GetMapping("/{id}")
    public ResponseEntity<TaskNotification> getNotification(
            @Parameter(description = "通知ID") @PathVariable Long id) {
        TaskNotification notification = taskNotificationService.getById(id);
        if (notification == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(notification);
    }


}